package wsclients;

import java.util.List;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.jdom.Element;
import org.jdom.Namespace;

import com.avetti.commerce.webservices.ItemService;
import com.avetti.commerce.webservices.Items;

public class ItemServiceTest {

    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }

    public void test(String[] args) throws Exception {

        String url = "http://localhost/t85prod/services/itemservice";

        System.out.println("plealse input the vendorid");
        
        String vid ="20140502077";

        System.out.println("plealse input the admin name");
        String admin = "avetti_wei_ws";//new BufferedReader(new InputStreamReader(System.in)).readLine();

        System.out.println("plealse input the password");
        String pwd = "1234";//new BufferedReader(new InputStreamReader(System.in)).readLine();

        Service serviceModel = new ObjectServiceFactory().create(ItemService.class);
        ItemService service = (ItemService) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        List<Items> loadi=service.findItemsAfterCurrentId(11063, 1);
        out(loadi.size());
    }
    
    private void out(Object obj){
    	System.out.println("!!!!!!!!!!!!!!  " + obj.toString());
    }

    private class VendorIdHandler extends AbstractHandler {
        private String vendorId;

        VendorIdHandler(String vendorId) {
            super();
            this.vendorId = vendorId;
        }

        public String getVendorId() {
            return vendorId;
        }

        public void setVendorId(String vendorId) {
            this.vendorId = vendorId;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:vendor");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element vendor = new Element("VendorToken", ns);
            Element vendor_el = new Element("VendorId", ns);
            vendor_el.addContent(vendorId);
            vendor.addContent(vendor_el);
            header.addContent(vendor);
        }
    }

    private class UserPassHandler extends AbstractHandler {
        private String user;
        private String pass;

        UserPassHandler(String user, String pass) {
            super();
            this.user = user;
            this.pass = pass;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:authentication");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element root = new Element("AuthenticationToken", ns);
            Element username_el = new Element("Username", ns);
            Element password_el = new Element("Password", ns);

            username_el.addContent(user);
            password_el.addContent(pass);
            root.addContent(username_el);
            root.addContent(password_el);

            header.addContent(root);
        }
    }
    
    public void testC76(String[] args) throws Exception {

        String url = "http://xx/preview/services/itemservice";

        System.out.println("plealse input the vendorid");
        
        String vid ="20070723002";

        System.out.println("plealse input the admin name");
        String admin = "webserviceuser";//new BufferedReader(new InputStreamReader(System.in)).readLine();

        System.out.println("plealse input the password");
        String pwd = "1234qwer";//new BufferedReader(new InputStreamReader(System.in)).readLine();

        Service serviceModel = new ObjectServiceFactory().create(ItemService.class);
        ItemService service = (ItemService) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        Items loadi=service.find(3595L);
        out("status code="+loadi.getStatuscode());
        
        loadi.setStatuscode(8);
        loadi.setTitle("TEST6_sku");
        
        service.update(loadi);
    }

    public static void main(String[] args) throws Exception {
        ItemServiceTest cst=new ItemServiceTest();
        cst.test(args);
        //cst.testC76(args);
    }
}
